<template>
  <div class="artical">
    <!-- 文章搜索表单 -->
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>条件查询</span>
      </div>
      <el-form ref="form" :model="form" label-width="80px" size="small">
        <el-row :gutter="20">
          <el-col :span="6" :offset="0">
            <el-form-item label="作者">
              <el-input v-model.trim="form.author" placeholder="作者" />
            </el-form-item>
          </el-col>
          <el-col :span="6" :offset="0">
            <el-form-item label="标题">
              <el-input v-model.trim="form.title" placeholder="标题" />
            </el-form-item>
          </el-col>
          <el-col :span="6" :offset="0">
            <el-form-item label="编辑类型">
              <el-select v-model="form.editorType" style="width:100%;" placeholder="编辑器类型" clearable filterable>
                <el-option
                  label="富文本"
                  :value="0"
                />
                <!-- editorType (integer, optional): 编辑器类型: 0 富文本; 1 markdown , -->
                <el-option
                  label="markdown"
                  :value="1"
                />
              </el-select>

            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12" :offset="18">
            <el-form-item>
              <el-button @click="resetForm">重置</el-button>
              <el-button type="primary" @click="searchByForm">搜索</el-button>
            </el-form-item>
          </el-col>
        </el-row>

      </el-form>

    </el-card>
    <!--文章表格 -->
    <el-card shadow="never" style="margin-top:40px">
      <div slot="header">
        <el-button type="primary" size="small" @click="goToUpdateArtiacl('')">新增</el-button>

      </div>
      <el-table v-loading="loading" :data="articalList" border stripe>
        <el-table-column
          fixed="left"
          align="center"
          type="index"
          label="#"
          width="80"
        />
        <el-table-column
          align="center"
          prop="title"
          label="文章标题"
          width="200"
        />
        <el-table-column
          align="center"
          label="展示图片"
          width="180"
        >
          <template slot-scope="scope">
            <img style="width:100px;height:100px;" :src="scope.row.coverImg" alt="">
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          prop="createTime"
          label="添加时间"
          width="200"
        />
        <el-table-column
          align="center"
          prop="author"
          label="文章作者"
          width="180"
        />
        <el-table-column
          align="center"
          label="文章是否显示"
          width="180"
        >
          <template slot-scope="scope">
            <!-- isShow (integer, optional): 是否显示: 0 否; 1 是 , -->
            <el-switch v-model="scope.row.isShow" :active-value="1" :inactive-value="0" @change="updateArticalStatus(scope.row.id,scope.row.isShow)" />
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          label="内容"
          width="200"
        >
          <template slot-scope="scope">
            <el-button type="primary" plain size="mini">{{ scope.row.editorType===0?'富文本':'markdown' }}</el-button>

          </template>
        </el-table-column>
        <el-table-column
          fixed="right"
          align="center"
          label="操作"
          width="150"
        >
          <template slot-scope="scope">
            <!-- isShow (integer, optional): 是否显示: 0 否; 1 是 , -->
            <el-button
              icon="el-icon-view"
              type="text"
              plain
              size="small"
              style="border:none;"
              @click="goToUpdateArtiacl(scope.row.id)"
            >编辑文章</el-button>
            <el-button type="text" plain size="small" style="color:red;border:none;" @click="deleteArtical(scope.row.id)">删除</el-button>

          </template>
        </el-table-column>

      </el-table>
      <el-pagination
        style="margin-top:10px;"
        :current-page="pagination.start"
        :page-sizes="pagination.sizes"
        :page-size="pagination.limit"
        layout="total, sizes, prev, pager, next, jumper"
        :total="pagination.total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>

  </div>
</template>

<script>
import {
  findArticles as findArticlesApi,
  changeShowStatus as changeShowStatusApi,
  del as delApi
} from '@/api/content/index'
import mix from '@/mixin/index'
export default {
  mixins: [mix],
  data() {
    return {
      articalList: [], // 文章数据
      form: {},
      loading: true
    }
  },
  created() {
    this.getArticalList()
  },
  methods: {
    // 删除文章
    deleteArtical(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        delApi(id)
          .then(res => {
            this.$message({
              type: 'success',
              message: '删除成功!'
            })
            this.getArticalList()
          })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 跳转到新增或者编辑文章页面
    goToUpdateArtiacl(articalId = '') {
      this.$router.push({ name: 'UpdateArtiacl', query: { articalId: articalId }})
    },
    // 通过表单搜索筛选文章
    searchByForm() {
      this.filterNullParamsObject(this.form)
      this.pagination.start = 1
      // 需要对form的内容做过滤
      this.getArticalList()
    },
    // 重置表单
    resetForm() {
      this.form = {}
    },
    // 更改页码
    handleCurrentChange(e) {
      this.pagination.start = e
      this.getArticalList()
    },
    // 更改size，即limit
    handleSizeChange(e) {
      this.pagination.start = 1
      this.pagination.limit = e
      this.getArticalList()
    },
    // 更新文章显示状态
    updateArticalStatus(id, isShow) {
      changeShowStatusApi({
        id: id,
        isShow: isShow
      })
    },
    // 获取文章列表
    getArticalList() {
      var data = null
      this.loading = true
      if (this.passForm) {
        data = [this.pagination.start, this.pagination.limit, this.form]
      } else {
        data = [this.pagination.start, this.pagination.limit]
      }

      findArticlesApi(...data)
        .then(res => {
          this.articalList = res.data.rows
          this.pagination.total = res.data.total
          this.passForm = false
          this.loading = false
        })
    }
  }
}
</script>

<style lang="scss" scoped>
.artical{
  padding: 40px;
  ::v-deep .el-form-item__label{
    font-weight: normal;
  }
}
</style>
